package com.fgba.express.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fgba.express.comm.GlobalConst;
import com.fgba.express.comm.enums.State;
import com.fgba.express.comm.exception.BizException;
import com.fgba.express.comm.result.ResultCode;
import com.fgba.express.core.controller.BaseController;
import com.fgba.express.data.dto.CourierDto;
import com.fgba.express.data.entity.Courier;
import com.fgba.express.data.entity.Courier;
import com.fgba.express.data.mapstruct.CourierStruct;
import com.fgba.express.data.search.CourierSearch;
import com.fgba.express.data.service.ICourierService;
import com.fgba.express.util.NullChecker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 快递员 前端控制器
 * </p>
 *
 * @author fgba
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/courier")
@Api(value = "快递员相关操作", tags = "快递员相关操作")
public class CourierController extends BaseController<Courier, CourierDto, CourierSearch, ICourierService, CourierStruct> {
    @Override
    public void createValidation(CourierDto model) {
        if (NullChecker.isNullOrZero(model.getUserId())) {
            log.error("CourierController -> save: userId is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }

        if (NullChecker.isNullOrEmpty(model.getName())) {
            log.error("CourierController -> save: name is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }

        if (NullChecker.isNullOrEmpty(model.getIdCardNumber())) {
            log.error("CourierController -> save: idCardNumber is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }
    }

    /**
     * 获取首页用户统计数据
     *
     * @return
     */
    @ApiOperation(value = "获取首页用户统计数据", notes = "获取首页用户统计数据")
    @GetMapping("/homeData")
    @PreAuthorize("isAuthenticated()")
    public Map<String, Object> getHomeData() {
        LocalDate today = LocalDate.now();
        int weekDay = today.getDayOfWeek().getValue();
        //本周的第一天
        LocalDate weekFirstDay = today.minus(weekDay, ChronoUnit.DAYS);
        Map<String, Object> data = new HashMap<>(3);
        List<Integer> registeredCouriersWeekData = new LinkedList<>();
        int registeredCouriersWeekTotal = 0;

        //统计每一天的数量
        for (int i = 0; i < GlobalConst.WEEK_DAY; i++) {
            //统计从周一开始，每次循环添加到下一天
            LocalDate day1 = weekFirstDay.plus(i, ChronoUnit.DAYS);
            LocalDate day2 = weekFirstDay.plus(i + 1, ChronoUnit.DAYS);
            //今天之后的数据不统计
            if (today.isBefore(day1)) {
                break;
            }
            int registeredCouriersNum = service.count(Wrappers.<Courier>lambdaQuery().ge(Courier::getCreateTime, day1).lt(Courier::getCreateTime, day2));
            registeredCouriersWeekTotal += registeredCouriersNum;
            registeredCouriersWeekData.add(registeredCouriersNum);
        }
        data.put("registeredCouriersWeekData", registeredCouriersWeekData);
        data.put("registeredCouriersWeekTotal", registeredCouriersWeekTotal);
        data.put("registeredCouriersTotal", service.count(Wrappers.<Courier>lambdaQuery().eq(Courier::getState, State.STATUS_AVAILABLE)));

        return data;
    }
}
